package com.neusoft.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.dto.ArticleCreateReq;
import com.neusoft.dto.ArticleQueryCondition;
import com.neusoft.dto.ArticleReq;
import com.neusoft.mapper.ArticlesMapper;
import com.neusoft.mapper.FavoriteMapper;
import com.neusoft.model.Articles;
import com.neusoft.model.Favorites;
import com.neusoft.model.Users;
import com.neusoft.response.ApiResponse;
import com.neusoft.service.IArticleService;
import com.neusoft.utils.UserUtils;
import com.neusoft.vo.Article;
import com.neusoft.vo.MultiArticles;
import com.neusoft.vo.SingleArticle;
import org.jeasy.random.EasyRandom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api")
public class ArticleController {
    @Autowired
    IArticleService iArticleService;
    @Autowired
    ArticlesMapper articlesMapper;
    @Autowired
    FavoriteMapper favoriteMapper;

    @PostMapping("/articles")
    public ApiResponse<SingleArticle> createArticle(@RequestBody ArticleCreateReq articleCreateReq) {
        return ApiResponse.success(iArticleService.createArticle(articleCreateReq.getArticle()));

    }

    @GetMapping("/articles/{slug}")
    public ApiResponse<SingleArticle> getArticle(@PathVariable("slug") String slug) {
        return ApiResponse.success(iArticleService.getArticle(slug));
    }

    @PutMapping("/articles/{slug}")
    public ApiResponse<SingleArticle> updateArticle(@PathVariable("slug") String slug, @RequestBody ArticleCreateReq articleCreateReq) {
        return ApiResponse.success(iArticleService.updateArticle(slug, articleCreateReq.getArticle()));
    }

    @GetMapping("/articles")
    public ApiResponse<MultiArticles> getMultiArticles(ArticleQueryCondition articleQueryCondition) {
        return ApiResponse.success(iArticleService.getMultiArticles(articleQueryCondition));
    }

    @PostMapping("/articles/{slug}/favorite")
    public ApiResponse<SingleArticle> favoriteArticle(@PathVariable("slug") String slug) {
        return ApiResponse.success(iArticleService.favoriteArticle(slug));
    }

    @DeleteMapping("/articles/{slug}/favorite")
    public ApiResponse<SingleArticle> unFavoriteArticle(@PathVariable("slug") String slug) {
        return ApiResponse.success(iArticleService.unFavoriteArticle(slug));
    }

    @GetMapping("/articles/feed")
    public ApiResponse<MultiArticles> getMultiArticlesFeed(ArticleQueryCondition articleQueryCondition) {
        return ApiResponse.success(iArticleService.getMultiArticlesFeed(articleQueryCondition));
    }

    @DeleteMapping("/articles/{slug}")
    public void deleteArticle(@PathVariable("slug") String slug) {
        QueryWrapper<Articles> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("slug", slug);
        Articles articles = articlesMapper.selectOne(queryWrapper);
        Users currentUser = UserUtils.getLoginUser();
        if (articles.getAuthorId() == currentUser.getId()) {
            articlesMapper.delete(queryWrapper);
        }
    }
}
